package com.xu.sms.net;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;
import android.text.Html;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.xu.sms.R;
import com.xu.sms.entity.Info;
import com.xu.sms.entity.Type;
import com.xu.sms.utils.DateUtil;
import com.xu.sms.utils.LogUtil;
import com.xu.sms.utils.TypePictureUtil;

public class HTTPNet {
	
	public static String servet_IP_Address = "www.xupenghao.xin:9998";


	/*
	 * 请求有
	 * 1.根据类型去请求子类型
	 * 2.根据类型来请求子信息
	 */
	public static String  RESOURCE_URL = "http://"+servet_IP_Address+"/sms/Path";
	public static final int FIND_TYPE_BY_PARENT_STATE = 1;
	public static final int FIND_INFO_BY_TYPE_STATE = 2;
	public static final int FIND_INFO_TYPE_BIG_CUR_DATE_STATE = 3;
	public static final int FIND_INFO_BY_KEYWORD_STATE = 4;
	
	private final static String TAG = HTTPNet.class.getSimpleName();

	public static void requestServerForGet(final Handler handler,final String requestUrl){
		AsyncHttpClient async = new AsyncHttpClient();
LogUtil.i(TAG,40,requestUrl);
		async.get(requestUrl, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {
				try {
					if(statusCode ==200){
						String result = new String(responseBody,"utf-8");
						Message msg = Message.obtain();
						JSONArray jsonArray = new JSONArray(result);
						//设置msg的数据
						setMsgData(requestUrl, msg, jsonArray);
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			private void setMsgData(final String requestUrl, Message msg,
					JSONArray jsonArray) throws JSONException {
				//解析json字符串
				if(requestUrl.contains("findTypeByParentId")){
					//解析根据父类型得到子类型
					List<Type> typeList = setTypeBeanData(jsonArray);
					msg.what = FIND_TYPE_BY_PARENT_STATE;
					msg.obj = typeList;
				}else if(requestUrl.contains("findInfoByTypeId")){
					//解析根据类型得到指定的信息条目
					List<Info> infoList = setInfoBeanData(jsonArray);
					msg.what = FIND_INFO_BY_TYPE_STATE;
					msg.obj = infoList;
				}else if(requestUrl.contains("findTypeBigCurDate")){
					List<Type> typeList = setTypeBeanData(jsonArray);
					msg.what = FIND_INFO_TYPE_BIG_CUR_DATE_STATE;
					msg.obj = typeList;
				}else if(requestUrl.contains("findInfoBykeyWord")){
					List<Info> infoList = setInfoBeanData(jsonArray);
					msg.what = FIND_INFO_BY_KEYWORD_STATE;
					msg.obj = infoList;
				}
			}
			
			private List<Info> setInfoBeanData(JSONArray jsonArray)throws JSONException {
				 List<Info> infoList = new ArrayList<Info>();
				for(int i=0;i<jsonArray.length();i++){
					String infoContent = (String)jsonArray.get(i);
					//Html.fromHtml(source);
				    Info info = new Info(infoContent );
				    infoList.add(info);
				}
				LogUtil.v(TAG, 76,infoList.size()+"");
				return infoList;
			}
			
			private List<Type> setTypeBeanData(JSONArray jsonArray)
					throws JSONException {
				List<Type> typeList = new ArrayList<Type>();
				for(int i=0;i<jsonArray.length();i++){
					JSONObject jsonObj = jsonArray.getJSONObject(i);
					int typeId = jsonObj.getInt("typeId");
					String typeName = jsonObj.getString("typeName");
					Date typeTime =  DateUtil.strChangeDate(jsonObj.getString("typeTime"));
					String typeImgurl = jsonObj.getString("typeImgurl");
					String typeDescription = jsonObj.getString("typeDescription");
					int imgResId = TypePictureUtil.getLocalResourceImgId(TypePictureUtil.getImgName(typeImgurl));
					//typeImgurl = RESOURCE_URL+ typeImgurl;
				    Type type = new Type(typeId,typeName,typeTime,imgResId,typeDescription);
				    typeList.add(type);
				}
				return typeList;
			}
			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
					Throwable error) {
				LogUtil.e(TAG, 102,"请求失败");
				
			}
		});
	}
}
